//
//  XNBaseNavController.swift
//  weibo
//
//  Created by 王向宁 on 16/5/20.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit

class XNBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //解决又滑返回的手势问题
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    //重写push方法
    //XNBaseNavController(rootViewController: vc) 内部会进行push操作
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        var backtitle = "返回"
        
        if childViewControllers.count > 0 {
            
            if childViewControllers.count == 1 {
                backtitle = childViewControllers.first?.title ?? "返回"
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backtitle, target: self, action: #selector(popToFrontVc))
            
            viewController.navigationItem.leftBarButtonItem = back
            
            viewController.hidesBottomBarWhenPushed = true
            
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func popToFrontVc() {
        popViewControllerAnimated(true)
    }
    
    //实现手势的协议方法
    //一般协议方法的返回值为bool类型,表示如果返回true就表示对象能够正常工作,如果返回false表示不能正常工作
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

   

}
